package com.cssnj.portal.service.home;

import com.cssnj.portal.domain.common.UserInfo;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.service.base.BaseService;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.*;

/**
 * @author : Arthur
 * @since : create on 2018-8-28 15:54
 */
@Service
public class HomeService extends BaseService {

    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        switch (jsonRequest.getHandleCode()) {
            case "zxInfo":
                return zxInfo(jsonRequest);
            case "getTjGn":
                return getTjGn(jsonRequest);
        }
        return null;
    }


    protected JsonResponse zxInfo(JsonRequest jsonRequest) {

        JsonResponse jsonResponse = new JsonResponse();

        UserInfo userInfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));
        List<String> params = new ArrayList<String>();
        params.add(userInfo.getSwrySfDm());

        List<Map<String, Object>> list = this.queryDao.queryForList("SELECT ZXLX_DM FROM GL_YH_SY WHERE SWRYSF_DM  =? ", params);

        String zxlx_dm = "1"; //默认展现类型 1
        if (list.size() > 0) {
            zxlx_dm = (String) list.get(0).get("ZXLX_DM");
        }
        jsonResponse.getRepData().put("zxlx_dm", zxlx_dm);

        if ("1".equals(zxlx_dm) || "3".equals(zxlx_dm)) { // 查询收藏功能
            params.clear();
            params.add(userInfo.getSwrySfDm());
            String sql = "SELECT A.GN_ID, B.SCJ_ID, C.GN_MC, C.JDYS, C.HREF \n" +
                         "  FROM GL_GN_FAV A, GL_GN_SCJ B, GL_GN C          \n" +
                         " WHERE A.YH_ID = B.YH_ID  \n" +
                         "   AND A.SCJ_ID =B.SCJ_ID \n" +
                         "   AND A.YH_ID = ?        \n" +
                         "   AND A.GN_ID = C.GN_ID  \n" +
                         "   AND C.XY_BJ = '1'      \n" ;
            List<Map<String, Object>> favList = this.queryDao.queryForList(sql, params);
            List<Map<String, Object>> sjcGnList = this.queryDao.queryForList("SELECT SCJ_ID,SCJ_MC  FROM GL_GN_SCJ WHERE YH_ID=?  ORDER BY PXXH DESC,LR_SJ DESC", params);

            for (int i = 0; i < sjcGnList.size(); i++) {
                String scj_id = sjcGnList.get(i).get("SCJ_ID").toString();
                List<Map<String, Object>> gnlist = new ArrayList<Map<String, Object>>();
                for (int j = 0; j < favList.size(); j++) {
                    Map<String, Object> map = favList.get(j);
                    if (scj_id.equals(map.get("scj_id").toString())) {
                        gnlist.add(map);
                    }
                }
                sjcGnList.get(i).put("gnList", gnlist);
            }

            jsonResponse.getRepData().put("sjcGnList", sjcGnList);

        }

        if ("2".equals(zxlx_dm) || "3".equals(zxlx_dm)) { //查询图表布局

            String mbid = null; //默认展现类型 1
            String sfjgjcdm = userInfo.getSfJgjcDm();;

            params.clear();
            params.add(userInfo.getSwrySfDm());
            params.add(sfjgjcdm);

            list = this.queryDao.queryForList("  SELECT MB_ID MBID FROM GL_YH_SY_ZXMB T WHERE SWRYSF_DM  =? AND MB_ID IN (SELECT MB_ID  FROM GL_JGJC_ZXMB G  WHERE JGJC_DM=?  OR JGJC_DM='ALL' ) AND EXISTS ( SELECT * FROM DM_GL_ZXMB K WHERE K.XY_BJ='1' AND T.MB_ID=K.MB_ID )   ",params);
            if(list.size()>0){
                mbid=(String)list.get(0).get("MBID");
            }

            if(mbid==null){
                params.clear();
                params.add(sfjgjcdm);
                list = this.queryDao.queryForList(" SELECT MB_ID MBID FROM GL_JGJC_ZXMB T WHERE JGJC_DM=? AND ISMR='1'  AND EXISTS ( SELECT * FROM DM_GL_ZXMB K WHERE K.XY_BJ='1' AND T.MB_ID=K.MB_ID ) ",params);
                if(list.size()>0){
                    mbid=(String)list.get(0).get("MBID");
                }
            }

            if(mbid==null) {
                params.clear();
                list = this.queryDao.queryForList(" SELECT MB_ID MBID FROM GL_JGJC_ZXMB T  WHERE JGJC_DM='ALL' AND ISMR='1' AND EXISTS ( SELECT * FROM DM_GL_ZXMB K WHERE K.XY_BJ='1' AND T.MB_ID=K.MB_ID )  ", params);
                if (list.size() > 0) {
                    mbid = (String) list.get(0).get("MBID");
                }
            }

            if(mbid==null) {
                mbid="1";
            }

            Map<String, Object> sqlparam = new HashMap<String, Object>();
            sqlparam.put("MB_ID", mbid);
            sqlparam.put("SFDM", userInfo.getSwrySfDm());
            sqlparam.put("GW_DM", this.getGwParams(userInfo));
            List<Map<String, Object>> bjList = this.queryDao.queryForList(
                    " SELECT G.MB_ID MBID, \n" +
                        "   G.WZ_ID WZID,     \n" +
                        "   G.COLS,           \n" +
                        "   G.TBYY_ID,        \n" +
                        "   T.TBYY_MC,        \n" +
                        "   T.TBYY_DZ,        \n" +
                        "   K.YY_GW||T.TBYY_HREF TBYY_HREF \n" +
                        "  FROM DM_GY_TBYY T, \n" +
                        "       (SELECT A.MB_ID, A.WZ_ID, A.COLS, NVL(B.TBYY_ID, A.TBYY_ID) TBYY_ID\n" +
                        "          FROM (SELECT A.* FROM GL_ZXMB_BJ A  WHERE A.MB_ID = :MB_ID ) A,\n" +
                        "               (SELECT A.* FROM GL_YH_SY_BJ A WHERE A.MB_ID = :MB_ID AND SWRYSF_DM=:SFDM" +
                        "                    AND A.TBYY_ID NOT IN ( SELECT DISTINCT TBYY_ID FROM GL_GW_TBYY WHERE GW_DM NOT IN (:GW_DM) )" +
                        "                ) B \n" +
                        "         WHERE A.MB_ID = B.MB_ID(+)\n" +
                        "           AND A.WZ_ID = B.WZ_ID(+)) G," +
                        "           YY_JCXX K \n" +
                        " WHERE G.TBYY_ID = T.TBYY_ID(+)" +
                        "   AND T.YY_ID = K.YY_ID(+)", sqlparam);

            jsonResponse.getRepData().put("bjList", bjList);

            params.clear();
            params.add(mbid);
            list = this.queryDao.queryForList(
                    "SELECT B.ROWN , B.COLN COLN\n" +
                            "  FROM DM_GL_ZXMB B\n" +
                            " WHERE B.MB_ID = ?", params);
            if(list.size()>0){
                jsonResponse.getRepData().put("rownum", list.get(0).get("ROWN"));
                jsonResponse.getRepData().put("colnum", list.get(0).get("COLN"));
            }

        }

        return jsonResponse;
    }

    public JsonResponse getTjGn(JsonRequest jsonRequest) throws SQLException, BaseException {

        JsonResponse jsonResponse = new JsonResponse();

        UserInfo userinfo = (UserInfo) jsonRequest.getReqData().get(environment.getProperty(SysConstants.AUTH_SESSION_NAME));

        String sql =
                "SELECT DISTINCT B.GN_ID,B.GN_MC,B.HREF,B.JDYS,XT_DM SYSCODE\n" +
                        "  FROM CS_GY_TJGN A, GL_GN B\n" +
                        " WHERE A.GN_ID = B.GN_ID\n" +
                        "   AND GW_DM IN (:GW_DM)\n" +
                        "   AND B.XY_BJ='1' \n" +
                        "   AND A.XY_BJ='1' " ;

        Map<String, Object> sqlparam = new HashMap<String, Object>();
        sqlparam.put("GW_DM", this.getGwParams(userinfo));
        List<Map<String, Object>> list = this.queryDao.queryForList(sql, sqlparam);
        //我查询到添加默认推荐
        if(list.size()==0){
            sqlparam.clear();
            list=this.queryDao.queryForList("SELECT DISTINCT B.GN_ID,B.GN_MC,B.HREF,B.JDYS,XT_DM SYSCODE \n" +
                    "  FROM CS_GY_TJGN A, GL_GN B  \n" +
                    " WHERE A.GN_ID = B.GN_ID      \n" +
                    "   AND GW_DM ='mrtjgn'        \n" +
                    "   AND B.XY_BJ='1'      \n" +
                    "   AND A.XY_BJ='1' ", sqlparam);
        }

        jsonResponse.getRepData().put("tjgnList", list);
        return jsonResponse;
    }

    private List<String>  getGwParams(UserInfo userInfo){
        List<Map<String, Object>> gwList = userInfo.getGwlist();
        List<String> gwParams = new ArrayList<String>();
        for (int i = 0; i < gwList.size(); i++) {
            gwParams.add((String) gwList.get(i).get("GW_DM"));
        }
        return gwParams;
    }

}
